﻿using BaseData.Exts;
using BaseData.Models;
using BaseLib.Helper;
using BaseUtility;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebUI.Ashx.Acc.Staff
{
    /// <summary>
    /// GroupAdminInfoList 的摘要说明
    /// </summary>
    public class GroupAdminInfoList : Base, IHttpHandler
    {
        private static readonly AccClass _accId = new AccExts(new GetAccType().Parse(Request.QueryString["url"])).Instances;
        public void ProcessRequest(HttpContext context)
        {
            if (_accId.Id == 0 || string.IsNullOrWhiteSpace(_accId.Name) || _accId.Type == 0) return;
            using (var ctx = new ContextExt())
            {
                var list = new List<DataTableClass.Convert>
                {
                    new DataTableClass.Convert() { Assembly = new Web_Base_News_Info().TToAssembly(), fix = "Info", },
                    new DataTableClass.Convert() { Assembly = new Web_Base_News_Info_Ext().TToAssembly(), fix = "Info", skip = new string[] { "PkId", "NodeId" }, },
                };
                var dtNew = list.IListConvertToDataTable();
                // 1.主查询开始。
                var sql = ctx.Web_Base_News_Info.Where(q => (q.ParentId ?? 0) == 0);
                var res = ctx.Web_Base_Exts_Resx.Where(q => (q.ImagesCount ?? 0) != 0);
                sql = sql.Where(w => res.Select(s => s.NodeId ?? 0).Contains(w.PkId));
                var items = new List<Web_Base_News_Info>();
                if (sql.Any()) items = sql.ToList();
                // 2.主查询结果插入DataTable。
                dtNew.Rows.Add(items.ToList().TFillDataRow(dtNew.NewRow()));
                // 3.修改DataTable的相关项。
                foreach (var item in items)
                {
                    var dr = dtNew.NewRow();
                    dr = item.TFillDataRow(dr);
                    dr["id"] = item.PkId;
                    dr["count"] = !string.IsNullOrEmpty(item.TitleExt) ||
                                  !string.IsNullOrEmpty(item.ContentExt)
                        ? 1
                        : 0;
                    dtNew.Rows.Add(dr);
                }
                context.Response.Write(dtNew.ObjectsToJson());
            }
            context.Response.ContentType = "text/plain";
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}